#include <iostream>
#include <vector>
using namespace std;

class Solution {
  public:
    int surfaceArea(vector<vector<int>>& grid) {
        if (grid.empty()) return 0;
        int area = 0;
        for (auto row : grid) {
            for (auto n : row) {
                area += ((n != 0) ? (4 * n + 2) : 0);
            }
            std::cout << area << std::endl;
        }

        for (int i = 0; i < grid.size(); ++i) {
            for (int j = 0; j < grid[0].size() - 1; ++j) {
                area -= std::min(grid[i][j], grid[i][j + 1]) * 2;
            }
        }
        std::cout << area << std::endl;

        for (int i = 0; i < grid.size() - 1; ++i) {
            for (int j = 0; j < grid[0].size(); ++j) {
                area -= std::min(grid[i][j], grid[i + 1][j]) * 2;
            }
        }
        std::cout << area << std::endl;

        return area;
    }
};

int main() {
    std::vector<std::vector<int>> grid{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
    Solution s;
    s.surfaceArea(grid);
}
